package org.tinyweb.render;

import java.io.IOException;
import java.io.PrintWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class HtmlRender extends Render{
    private static final Logger LOGGER = LoggerFactory.getLogger(HtmlRender.class);
    private static final String CONTENT_TYPE = "text/html; charset=" + getEncoding();
    private String text;
    
    public HtmlRender(String text){
        this.text = text;
    }
    
    @Override
    public void render() {
        PrintWriter writer = null;
        try {
            response.setHeader("Pragma", "no-cache");   // HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            
            response.setContentType(CONTENT_TYPE);
            writer = response.getWriter();
            writer.write(text);
            writer.flush();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        } finally {
            if (writer != null){
                writer.close(); 
            }
        }
    }
    
}
